home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / simul.c < prev    next >
C/C++ Source or Header  |  1996-07-30  |  3KB  |  143 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <unistd.h>
  4. #include "../misc/misc.h"
  5. #include "../xconf/xconf.h"
  6. #include "netconf.h"
  7. #include "netconf.m"
  8.  
  9. static NETCONF_HELP_FILE help_simul ("simul");
  10.  
  11. /* #Specification: netconf / simulation mode
  12.     netconf can probe the environnement and compare to the
  13.     current configuration. If it find some differences between
  14.     the active setup and the requiered configuration, it will
  15.     issue different commands to bring the system in sync with
  16.     the configuration.
  17.  
  18.     One nice feature of netconf is that it can simulate all
  19.     this and record all commands that would have been executed.
  20.     This feature is mainly used by netconf which do a simulation
  21.     before quitting from the interactive mode. If the simulation
  22.     record any commands, netconf prompt the user and let him
  23.     do a "netconf --update" interactivly.
  24. */
  25.  
  26. static FILE *fout = NULL;
  27. static char do_close;
  28. /*
  29.     Start the simulation mode. Initialise the record buffer.
  30. */
  31. void simul_init()
  32. {
  33.     if (fout == NULL){
  34.         do_close = 1;
  35.         fout = xconf_fopen (TMP_SIMUL,"w");
  36.     }
  37. }
  38.  
  39. void simul_init (FILE *f)
  40. {
  41.     fout = f;
  42.     do_close = 0;
  43. }
  44.  
  45. static char demo_mode = 0;
  46. /*
  47.     Set the demo flag.
  48.     When active, linuxconf always tell that we are doing a simulation.
  49. */
  50. void simul_setdemoflag (int on)
  51. {
  52.     demo_mode = (char)(on!=0);
  53. }
  54.  
  55. /*
  56.     Return != 0 if we are running in demo mode
  57. */
  58. int simul_isdemo()
  59. {
  60.     return demo_mode;
  61. }
  62. /*
  63.     Return != 0 if netconf is in simulation mode
  64. */
  65. int simul_ison()
  66. {
  67.     return fout != NULL || demo_mode;
  68. }
  69.  
  70. /*
  71.     Record one shell command that would have been executed.
  72. */
  73. void simul_addcommand(const char *cmd)
  74. {
  75.     if (fout != NULL) fprintf (fout,"Execute %s\n",cmd);
  76. }
  77. /*
  78.     Record one comment.
  79. */
  80. void simul_addmsg(const char *ctl, ...)
  81. {
  82.     if (fout != NULL){
  83.         va_list list;
  84.         va_start (list,ctl);
  85.         vfprintf (fout,ctl,list);
  86.         va_end (list);
  87.         fflush (fout);
  88.     }
  89. }
  90.  
  91. /*
  92.     Prompt the user, if needed after the simulation.
  93.     Return 1 if an update of the system is need and the user
  94.     has accepted to do it.
  95.  
  96.     Return 0 if there was nothing to do at all.
  97.     Return -1 if there was something to do but the user escaped away.
  98.     Return 1 if the user want to do it.
  99. */
  100. int simul_prompt()
  101. {
  102.     int ret = 0;
  103.     if (fout != NULL){
  104.         int not_empty = ftell(fout) > 0;
  105.         if (do_close) fclose (fout);
  106.         fout = NULL;
  107.         if (not_empty){
  108.             int choice = 0;
  109.             while (1){
  110.                 static const char *menuopt[]={
  111.                     MSG_U(M_ACTIVATE,"Activate"),    MSG_U(M_THECHANGES,"the changes"),
  112.                     MSG_U(M_PREVIEW,"Preview"),    MSG_U(M_WHATHAS,"what has to be done"),
  113.                     NULL
  114.                 };
  115.                 MENU_STATUS code = xconf_menu (
  116.                     MSG_U(T_STATUS,"Status of the system")
  117.                     ,MSG_U(I_STATUS,"The state of the system is not\n"
  118.                      "in sync with the current/updated\n"
  119.                      "configuration. You are allowed to\n"
  120.                      "make it current, or continue with\n"
  121.                      "the current configuration. You can also\n"
  122.                      "look at the things that will have to be done\n"
  123.                      "to make the system current.\n")
  124.                     ,help_simul
  125.                     ,menuopt,choice);
  126.                 if (code != MENU_OK){
  127.                     ret = -1;
  128.                     break;
  129.                 }else if (choice == 0){
  130.                     ret = 1;
  131.                     break;
  132.                 }else if (choice == 1){
  133.                     dialog_textbox (MSG_U(T_THINGSTODO,"Things to do")
  134.                         ,TMP_SIMUL);
  135.                 }
  136.             }
  137.         }
  138.         unlink (TMP_SIMUL);
  139.     }
  140.     return ret;
  141. }
  142.  
  143.